我试图在单击通知时单击事件。
我拥有的
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Test";
Notification mNotification = new Notification(R.drawable.notification_template_icon_bg, MyText, System.currentTimeMillis() );
String MyNotificationTitle = "Test!";
String MyNotificationText = "Test!";
Intent MyIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
makeToast(StartIntent.getIntentSender().toString());
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
notificationManager.notify( NOTIFY_ME_ID, mNotification);
这工作得很好,但我不知道该怎么做,就是点击该通知。
我尝试过的
我尝试做一些事情onUserInteraction()
如果我没记错的话,当 Intent 开始一项新活动时似乎会被解雇,但没有起作用。
我也尝试过onActivityResult()
但我不知道如何获得当前的意图。
我尝试的最后一件事是做类似的事情this
BroadcastReceiver call_method = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action_name = intent.getAction();
if (action_name.equals("MyIntent")) {
//STUFF HERE
}
};
};
registerReceiver(call_method, new IntentFilter("MyIntent"));
也可以代替 putMyIntent
那就是Intent
,我试图把PendingIntent
但不起作用。
顺便说一句,当我尝试创建一个时,我的代码中会出现这个Notification
当我尝试打电话时setLatestEventInfo()
但我不知道这是否是问题的原因,或者是否会在将来带来问题。
我做错了什么?
EDIT
我刚刚创建了一个示例,该示例演示了我的应用程序目前的功能。很简单,当我按 aButton
它会弹出一个Notification
。在我真正的应用程序上,我不必单击Button
但都是一样的。我想要的是获得点击事件Notification
并利用该事件进行制作。我所做的就是创建另一个Activity
我把想要的东西放在那里onCreate()
在我想做的事情结束时我打电话给Finish()
完成该操作的方法Activity
,但我不知道这是否是最好的方法。我想要另一种方法来做到这一点我不想使用两种Activities
...
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnoti;
private static final int NOTIFY_ME_ID=1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnoti = (Button)findViewById(R.id.btnoti);
btnoti.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnoti){
addNotification();
}
}
private void addNotification() {
//We get a reference to the NotificationManager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Test";
Notification mNotification = new Notification(R.mipmap.ic_launcher, MyText, System.currentTimeMillis() );
String MyNotificationTitle = "Test!";
String MyNotificationText = "Test!";
Intent MyIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
notificationManager.notify( NOTIFY_ME_ID, mNotification);
}
编辑 2(三个快速问题)以继续我的代码...
我希望你不介意向我解决这三个快速问题......
- 从现在开始我已经使用了
Thread.sleep()
例如每 30 秒执行一项任务while(true)
但我不知道这是否是最好的方法,因为我想让用户选择时间,例如时间可以是 5 分钟或 5 小时...而且我不知道最好的方法是什么,我已经读到有一种方法或称为AlarmManager
重复任务的正确方法是?有没有任何源示例来了解如何使用它Alarm Manager
?
- 我必须知道用户何时创建“
finish()
“ 来自Intent
(ACTION_PICK_WIFI_NETWORK) 我的意思是当我关闭该应用程序后返回我的应用程序时Intent
我用过onResume()
但我不知道这是否是正确的工作方式,不是吗? (如果你不明白我想说的很简单,我想知道知道用户何时关闭 Wifi 网络选择器的事件的名称)
- 这是一种让你的应用程序在你转到另一个应用程序时仍然活着的方法吗?我的意思是,你可以转到另一个应用程序,而你的应用程序仍然可以在没有用户交互的情况下工作?因为从现在开始,如果我转到另一个应用程序,我的应用程序就像睡眠或其他什么一样,并且不会继续运行......
我读过一些东西来调用任务
Service
我认为一切顺利,即使该应用程序不在最近的应用程序中,它仍然可以运行......
谢谢,如果您不能回答我,我可以为每个问题发一个帖子,但我认为这些问题可以很快得到回答。